package com.oa.servlet;

import com.oa.entity.Employee;
import com.oa.service.EmployeeService;
import com.oa.service.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "EmpServlet",urlPatterns = "/EmpServlet")
public class EmpServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//需要处理多个请求，使用por参数判断请求的处理方式
		String opr = request.getParameter("opr");
		switch (opr){
			case "list":
				list(request, response);
				break;
			case "view":
				view(request, response);
				break;
			case "del":
				del(request, response);
				break;
			case "modifview":
				modifview(request, response);
				break;
		}
	}

	//所有
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println(this.getClass().getName());
		//1、获取 请求的参数
		String empName = request.getParameter("empName");

		//2、操作
		EmployeeService employeeService  = new EmployeeServiceImpl();
		List<Employee> list = employeeService.findParam(empName);
		System.out.println("empName="+empName+" "+list.size());
		//3、页面跳转==>判断是否需要传递数据 ==》转发/重定向
		request.setAttribute("empList",list);
		request.getRequestDispatcher("jsp/user/empl_list.jsp").forward(request, response);
	}

	//详情
	private void view(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println(this.getClass().getName());
		//1、获取 请求的参数
		String id = request.getParameter("id");

		//2、操作
		EmployeeService employeeService  = new EmployeeServiceImpl();
		Employee emp =  employeeService.findById(Integer.valueOf(id));
		//3、页面跳转==>判断是否需要传递数据 ==》转发/重定向
		request.setAttribute("emp",emp);
		request.getRequestDispatcher("jsp/user/empl_view.jsp").forward(request, response);
	}

	//删除
	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println(this.getClass().getName());
		//1、获取 请求的参数
		String id = request.getParameter("id");

		//2、操作
		EmployeeService employeeService  = new EmployeeServiceImpl();

	}

	//修改详情
	private void modifview(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println(this.getClass().getName());
		//1、获取 请求的参数
		String id = request.getParameter("id");
		//2、操作
		EmployeeService employeeService  = new EmployeeServiceImpl();
		Employee emp =  employeeService.findById(Integer.valueOf(id));
		//3、页面跳转==>判断是否需要传递数据 ==》转发/重定向
		request.setAttribute("emp",emp);
		request.getRequestDispatcher("jsp/user/empl_modify.jsp").forward(request, response);
	}

}
